package com.hy.web;

import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hy.domain.Cases;

@Controller
public class AnliController {
	
	@RequestMapping("anli")
	public String anli(Model model, @RequestParam(defaultValue = "1", required = false) int page) {
		long count = Cases.countCaseses();								//总数
		int pages = (int) (count % 9 > 0 ? count / 9 + 1 : count / 9);//总页数
		//大于总页数，或者页码小于0，重定向到第一页
		if(page > pages || page < 1){
			return "redirect:/anli.html?page=1";
		}
		int up = page > 1 && page < pages ? page - 1 : 1;				//上一页
		int next = page > 0 && page < pages ? page + 1 : 1;				//下一页
		int start = (int)(count - page * 9);							//起始位置
		int length = 9;													//读取条数
		start = start < 0 ? 0 : start;
		length = start == 0 ? (int)count % length : length; 
		List<Cases> cases = Cases.findCasesEntries(start, length);		
		Collections.reverse(cases);										//降序排列
		
		
		model.addAttribute("cases", cases);
		model.addAttribute("pages", pages);
		model.addAttribute("up", up);
		model.addAttribute("next", next);
		return "anli";
	}
	
	@RequestMapping("anli_xq")
	public String anli_xq(Model model, @RequestParam long id) {
		/*
		Cases cases = Cases.findCases(id);
		model.addAttribute("cases", cases);
		model.addAttribute("prev", cases.prev());
		model.addAttribute("next", cases.next());
		return "anli_xq";
		*/
		return "redirect:/anli/view/" + id + ".html";
	}
	
}
